/*
 * LineText.cpp
 *
 *  Created on: 2012-12-21
 *      Author: yangentao@gmail.com
 */

#include <assert.h>
#include "LineParse.h"

namespace yet {

LineParse::LineParse() {

}
LineParse::LineParse(string text) {
	parse(text);
}

LineParse::~LineParse() {
}
bool LineParse::endWithCrLf(int line) {
	string& s = arr[line];
	if (!s.empty()) {
		char last = s[s.length() - 1];
		return last == 10 || last == 13;
	}
	return false;
}
int LineParse::getLineCount() {
	return arr.size();
}
string LineParse::getLineTextFull(int line) {
	assert(line < arr.size());
	return arr[line];
}
string LineParse::getLineText(int line) {
	assert(line < arr.size());
	string text = arr[line];
	if (!text.empty()) {
		if (text[text.length() - 1] == 10 || text[text.length() - 1] == 13) {
			text = text.substr(0, text.length() - 1);
		}
	}
	if (!text.empty()) {
		if (text[text.length() - 1] == 10 || text[text.length() - 1] == 13) {
			text = text.substr(0, text.length() - 1);
		}
	}
	return text;
}
void LineParse::parse(string text) {
	s = text;
	if (s.empty()) {
		return;
	}
	string linetext;
	for (int i = 0; i < s.length(); ++i) {
		linetext.push_back(s[i]);
		if (s[i] == 13) { //\r
			if ((i + 1 < s.length()) && (s[i + 1] == 10)) { //\n
				linetext.push_back(s[i + 1]);
				++i;
			}
			arr.push_back(linetext);
			linetext = "";
		} else if (s[i] == 10) {
			arr.push_back(linetext);
			linetext = "";
		}
	}
	if (!linetext.empty()) {
		arr.push_back(linetext);
	}
}

} /* namespace yet */
